package cn.tj.wenda.cloud.interceptor;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * 用于token转发
 * wenda
 */
public class JwtFeignInterceptor implements RequestInterceptor  {

    @Override
    public void apply(RequestTemplate template) {

    	ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    	HttpServletRequest request = servletRequestAttributes.getRequest();

        Map<String, Collection<String>> headers = new LinkedHashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();

            Enumeration<?> e = request.getHeaders(key);
            List<String> values = new ArrayList<>();
            while (e.hasMoreElements()) {
                String val = (String) e.nextElement();//调用nextElement方法获得元素
                values.add(val);
            }
            headers.put(key, values);
        }
        //headers.put(ConstUtil.TOKEN,Arrays.asList(token));
        template.headers(headers);
        //template.header(ConstUtil.TOKEN, token);
    }

}
